#ifndef INET_CONNECTOR_H
#define INET_CONNECTOR_H
#include <map>
#include "EventHandler.h"
#include "InetAddress.h"
#include "Reactor.h"
#include "Handle.h"
#include "SocketConnector.h"

template<class SERVICE_HANDLER>
class Connector : public EventHandler
{
public:
   enum ConnectionMode {
      SYNC,
      ASYNC
   };
   
   Connector(Reactor *reactor)
     : reactor_(reactor)
   { };
   
   void connect(SERVICE_HANDLER *sh, const InetAddress &remoteAddr, ConnectionMode mode)
   {
     connect_service_handler(sh, remoteAddr, mode);
     activate_service_handler(sh);
   }

   Handle getHandle() 
   { 
     return 0;
   }
   
protected:
   virtual void connect_service_handler(SERVICE_HANDLER* sh,const InetAddress &addr, ConnectionMode mode)
   {
     connector_.open(addr);
     connector_.setStream(sh->peer());
   }

   int registerHandler(SERVICE_HANDLER *sh, ConnectionMode mode)
   {
     return 0;
   }

   virtual void activate_service_handler(SERVICE_HANDLER *sh)
   {
     sh->open();
   }

   virtual void complete(Handle)
   {

   }

private:
   typedef std::map<Handle, SERVICE_HANDLER*> ConnectionMap;
   ConnectionMap connectionMap_;
   SocketConnector connector_;
   Reactor *reactor_;
   virtual void handleEvent(Handle, EventType)
   {
    
   }
};


#endif
